/**
 * Note: The returned array must be malloced, assume caller calls free().
 */
int* findBall(int** grid, int gridSize, int* gridColSize, int* returnSize){
    int n = gridColSize[0];

    int* answer = (int *)malloc(sizeof(int) * n);

    for (int i = 0; i < n; i++)
    {
        int present = i;
        
        for (int j = 0; j < gridSize; j++)
        {
            int next = grid[j][present];
            present += next;
            if (present < 0 || present > n - 1 || next != grid[j][present])
            {
                present = -1;
                break;
            }
        }
        answer[i] = present;
    }
    *returnSize = n;

    return answer;

}